package com.wxisme.ssm.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wxisme.ssm.po.Album;
import com.wxisme.ssm.po.Photo;
import com.wxisme.ssm.po.User;
import com.wxisme.ssm.po.UserInfo;
import com.wxisme.ssm.service.AlbumService;
import com.wxisme.ssm.service.LoginService;
import com.wxisme.ssm.service.PhotoService;
import com.wxisme.ssm.service.UserInfoService;

/**
 *登录控制器
 *@author wxisme
 *@time 2015-9-6 上午11:08:25
 */
@Controller
public class LoginController {
	
	
	@Autowired
	private LoginService loginService;
	@Autowired
	private AlbumService albumService;
	@Autowired
	private PhotoService photoService;
	@Autowired
	private UserInfoService userInfoService;
	
	
	/**
	 * 进行登录处理
	 * @param session
	 * @param username
	 * @param password
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/login.action")
	public ModelAndView login(HttpSession session, HttpServletResponse response, HttpServletRequest request, String username, String password) throws Exception {
		
		
		ModelAndView modelAndView = new ModelAndView();
		//调用service进行认证
		boolean isCorrect = false;
		isCorrect = loginService.loginCheck(username, password);
		if(isCorrect) {
			//用户信息保存到session中
			session.setAttribute("username", username);
			
			if(session.getAttribute("wrong") != null) {
				session.removeAttribute("wrong");
			}
			
			//如果可以登录成功就加载用户的相册信息
			
			//1.加载用户的相册信息
			List<Album> albumList = albumService.selectAlbumByUsername(username);
			
			Map<Album, List<Photo>> userLoadingMap = new HashMap<Album, List<Photo>>();
			
			//加载用户每个相册的照片信息
			for(int i=0; i<albumList.size(); i++) {
				Album album = albumList.get(i);
				List<Photo> photoList = photoService.selectPhotoByAlbumPath(album.getPathName());
				userLoadingMap.put(album, photoList==null? (new ArrayList<Photo>()) : photoList);
			}
			
			//加载完毕  把加载好的数据放在session中。
			session.setAttribute("userLoadingMap", userLoadingMap);
			
			//加载头像
			String headname = userInfoService.selectHead(username);
			
			if(headname==null || headname.equals("")) {
				String head = "images/nophoto.jpg";
				session.setAttribute("head", head);
			}
			else {
				String head = "imgdata/" + username + "file/" + headname;
				session.setAttribute("head", head);
			}
			
			modelAndView.setViewName("personal");
			//response.sendRedirect("personal.jsp");
		}
		else {
			session.setAttribute("wrong", "wrong");
			response.sendRedirect("login.jsp");
			
		}
		
		
		
		return modelAndView;
		
		
	}
	
	/**
	 * 退出处理
	 * @param session
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/logout.action")
	public ModelAndView logout(HttpSession session, HttpServletResponse response) throws IOException {
		
		//清除session
		session.removeAttribute("username");
		
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("login");
		//直接重定向至login.jsp  地址栏变化
		//response.sendRedirect("login.jsp");
		
		return modelAndView;
	}
	
	/**
	 * 注册账号
	 * @param session
	 * @param request
	 * @param response
	 * @param username
	 * @param password
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/register.action")
	public ModelAndView register(HttpSession session, HttpServletRequest request,HttpServletResponse response, String username, String password) throws Exception {
		
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		
		ModelAndView modelAndView = new ModelAndView();
		
		
		//调用service进行业务处理
		boolean isUserExists = loginService.insertUserByRegister(user);
		//isUserExits为false时用户名可用
		if(!isUserExists) {
			//将用户信息保存到session中
			session.setAttribute("username", username);
			
			//如果可以注册成功，则完善UserInfo
			UserInfo userInfo = new UserInfo();
			userInfo.setAlbumNumber(0);
			userInfo.setPhotoNumber(0);
			userInfo.setUsername(username);
			Date date = new Date();
			userInfo.setRegisterTime(date.getTime());
		    //用用户名+file来唯一标示用户的文件
			String userPath = username + "file";
			
			
			
			userInfo.setUserPath(userPath);
			loginService.insertUserInfoByRegister(userInfo);
			//为新注册的用户创建文件夹   获取当前路径！！！！！
			String filePath = request.getServletContext().getRealPath("/") + "/imgdata/" + userPath;
			
			File userFile = new File(filePath);
			
			userFile.mkdir();
			
			
			//为注册用户的session中添加加载Map
			Map<Album, List<Photo>> userLoadingMap = new HashMap<Album, List<Photo>>();
			session.setAttribute("userLoadingMap", userLoadingMap);
			
			//加载头像
			session.setAttribute("head", "images/nophoto.jpg");
			
			//如果注册成功移除用户已存在的session值
			if(session.getAttribute("isexist")!=null) {
				session.removeAttribute("isexist");
			}
			modelAndView.addObject("user", user);
			modelAndView.setViewName("personal");
		}
		else {
			//用户名不可用 如果register页面获取到isexit的session就判定为用户名已存在
			session.setAttribute("isexist", "yes");
			response.sendRedirect("register.jsp");
			
		}
		
		return modelAndView;
		
	}
	
	@RequestMapping("/test.action")
	public ModelAndView test(HttpSession session, String username) throws Exception {
		
		User user = new User();
		user.setUsername(username);
		
		
		//将用户信息保存到session中
		session.setAttribute("username", username);
		
		ModelAndView modelAndView = new ModelAndView();
		
		modelAndView.addObject("user", user);
		modelAndView.setViewName("personal");
		
		return modelAndView;
		
	}
	
	
	
	
	

}
